package com.euond.accv.gateway.service;

import com.euond.accv.gateway.domain.CustomerDevManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author: QiangLiu
 * @create: 2017-10-25 上午11:19
 * @version: 1.0
 **/
@Service("tokenService")
public class TokenService {

    @Autowired
    RedisTemplate redisTemplate;

    private final String CUSTOMER_DEV_MANAGER = "customer_dev_manager_";

    private final String CUSTOMER_DEV_MANAGER_TOKEN = "customer_dev_manager_token_";

    private final long EXPIRE_TIME = 7200 ;

    private final long OLD_EXPIRE_TIME = 120;

    /**
     * 插入令牌
     * @param token 令牌
     * @param customerDevManager 客户资料
     */
    public void push(String token, CustomerDevManager customerDevManager,String deviceNo){

        //查询老token是否存在，存在则设置有效期为120秒
        String customerDev = "";

        if(StringUtils.isNotBlank(deviceNo)){
            //设备编号不为空,则为设备的申请accessToken
            customerDev = CUSTOMER_DEV_MANAGER + customerDevManager.getDevId() + "_"
                    + customerDevManager.getDevPassword() + "_" + deviceNo;
        }else {
            //查询老token是否存在，存在则设置有效期为120秒
            customerDev = CUSTOMER_DEV_MANAGER + customerDevManager.getDevId() + "_" + customerDevManager.getDevPassword();
        }

        //
        String currentToken = (String)redisTemplate.opsForValue().get(customerDev);
        if(StringUtils.isNotBlank(currentToken)){
            redisTemplate.expire(currentToken,OLD_EXPIRE_TIME, TimeUnit.SECONDS); //防止申请新token，老的http请求正在后台，导致无效的问题
        }

        String newToken = CUSTOMER_DEV_MANAGER_TOKEN + token;

        //转换成map
        Map<String,String> map = new HashMap<>();
        map.put("id",customerDevManager.getId()+"");
        map.put("devId",customerDevManager.getDevId());
        map.put("customerNo",customerDevManager.getCustomerNo());
        map.put("status",customerDevManager.getStatus());
        map.put("devPassword",customerDevManager.getDevPassword());
        map.put("creator",customerDevManager.getCreator());

        //数据存在forValue ，设置有效期
        redisTemplate.opsForValue().set(newToken,map,EXPIRE_TIME, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set(customerDev,newToken,EXPIRE_TIME, TimeUnit.SECONDS);
    }

//    /**
//     * 查询所有Token,请勿滥用
//     */
//    public List<CustomerDevManager> findAll(){
//        //查询老token是否存在，存在则设置有效期为120秒
//        Set<String> tokens = redisTemplate.keys(CUSTOMER_DEV_MANAGER_TOKEN+"*");
//
//
//        List<CustomerDevManager> list = new ArrayList<>();
//        for(String token:tokens){
//            CustomerDevManager dev = (CustomerDevManager)redisTemplate.opsForValue().get(token);
//            if(dev != null){
//                list.add(dev);
//            }
//        }
//        return list;
//    }

    /**
     * 获取商户
     * @param token 令牌
     * @return
     */
    public Map<String,String> getCustomerDev(String token){
        String newToken = CUSTOMER_DEV_MANAGER_TOKEN + token;
        Map<String,String> devManager = (Map<String,String>)redisTemplate.opsForValue().get(newToken);
        return devManager;
    }
}
